//
//  UserAppoitmentViewController.swift
//  worldexchange
//
//  Created by xionghao on 2019/2/18.
//  Copyright © 2019 xionghao. All rights reserved.
//

import UIKit
import MJRefresh

class UserAppoitmentViewController: QMUICommonTableViewController {
    
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "order-cell") as! OrderTableViewCell
        cell.item = orders[indexPath.row]
        return cell
    }
    
    
    var orders:[OrderItem] = [OrderItem]()

    override func viewDidLoad() {
        super.viewDidLoad()
        setupView()

        // Do any additional setup after loading the view.
    }
    
    func setupView() {
        self.title = LanguageHelper.getString(key: "appointLabel")
        let nib = UINib(nibName: "OrderTableViewCell", bundle: nil)
        
        self.tableView.register(nib, forCellReuseIdentifier: "order-cell")
        self.tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            self.loadData()
        })
        self.tableView.mj_header.beginRefreshing()
        self.tableView.rowHeight = 248
        
    }
    
    func loadData() {
        let token = userData!.token
        provider.rx.request(WE.orders(token: token)).mapString()
            .subscribe(onSuccess: { (res) in
                self.orders.removeAll()

                if let orders = [OrderItem].deserialize(from: res) as? [OrderItem] {
                    self.orders.append(contentsOf: orders)
                    self.tableView.reloadData()
                }
                self.tableView.mj_header.endRefreshing()

            }) { (error) in
                self.tableView.mj_header.endRefreshing()
                
        }
    }
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return orders.count
    }
    
    


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
